﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Enter.Common;

namespace Enter.DataProxy.Cache.Configuration
{
    internal sealed class CacheConfigurationManager
    {
        /// <summary>
        /// 读取使用哪个缓存环境 微软 mem
        /// </summary>
        /// <returns></returns>
        public CacheTypeSettings GetCacheSettings()
        {
            CacheTypeSettings setting = new CacheTypeSettings();
            string xmlfile = EntlibCache.FileString;
            if (xmlfile.Length > 0)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xmlfile);
                string xml1 = string.Empty;
                XmlNode topm = xmldoc.SelectSingleNode("configuration/Settings");
                if (topm != null)
                {
                    foreach (XmlNode xn in topm.ChildNodes)
                    {
                        switch (xn.Name)
                        {
                            case "CachingType":
                                string typename = XmlAttributes(xn, "Type").ToLower();
                                try
                                {
                                    setting.Cachetype = (CacheType)Enum.Parse(typeof(CacheType), typename);
                                }
                                catch
                                {
                                    setting.Cachetype = CacheType.webcache;
                                }
                                break;
                        }
                    }
                }
            }
            return setting;

        }

        /// <summary>
        /// 读取微软缓存服务器配置
        /// </summary>
        /// <returns></returns>
        public CacheSetting GetVelocitySetting()
        {
            return GetSetting(CacheType.velocitycache);
        }

        /// <summary>
        /// 读取mem缓存服务器配置
        /// </summary>
        /// <returns></returns>
        public CacheSetting GetMemSetting()
        {
            return GetSetting(CacheType.memcache);
        }

        /// <summary>
        /// 读取NoSql缓存服务器配置
        /// </summary>
        /// <returns></returns>
        public CacheSetting GetNoSqlSetting()
        {
            return GetSetting(CacheType.nosql);
        }


        public CacheErrSet GetErrSetting()
        {
            CacheErrSet errset = new CacheErrSet();
            string xmlfile = EntlibCache.FileString;
            if (xmlfile.Length > 0)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xmlfile);
                XmlNode topm1 = xmldoc.SelectSingleNode("configuration/CacheSetting/ErrorCache");
                if (topm1 != null)
                {
                    foreach (XmlNode xn in topm1.ChildNodes)
                    {
                        switch (xn.Name)
                        {
                            case "EnableCheck":
                                if (xn.InnerText.ToString().ToLower() == "true")
                                {
                                    errset.EnableCheck = true;
                                }
                                break;
                            case "IntervalTime":
                                errset.IntervalTime = xn.InnerText.To<int>(errset.IntervalTime);
                                break;
                            case "Maximum":
                                errset.Maximum = xn.InnerText.To<int>(errset.Maximum);
                                break;
                        }
                    }
                }
            }

            return errset;

        }

        /// <summary>
        /// 读取缓存服务器基本配置
        /// </summary>
        /// <param name="cachetype"></param>
        /// <returns></returns>
        private CacheSetting GetSetting(CacheType cachetype)
        {
            CacheSetting CacheSet = new CacheSetting();
            string xmlfile = EntlibCache.FileString;
            if (xmlfile.Length > 0)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xmlfile);
                if (CacheSet != null)
                {
                    XmlNode topm1 = xmldoc.SelectSingleNode("configuration/CacheSetting");
                    if (topm1 != null)
                    {
                        CacheSet = new CacheSetting();

                        foreach (XmlNode xn in topm1.ChildNodes)
                        {
                            switch (xn.Name)
                            {
                                case "EnableCaching":
                                    if (xn.InnerText.ToString().ToLower() == "true")
                                    {
                                        CacheSet.EnableCaching = true;
                                    }
                                    break;
                                case "EnableLog":
                                    if (xn.InnerText.ToString().ToLower() == "true")
                                    {
                                        CacheSet.EnableLog = true;
                                    }
                                    break;
                                case "CachingTime":
                                    CacheSet.CachingTime = xn.InnerText.To<int>(CacheSet.CachingTime);
                                    break;
                                case "CacheName":
                                    CacheSet.CacheName = xn.InnerText;
                                    break;
                                case "VelocityCache":
                                    if (cachetype == CacheType.velocitycache)
                                    {
                                        CacheSet.CacheServers = VelocityServers(xn);
                                    }
                                    break;
                                case "MemCache":
                                    if (cachetype == CacheType.memcache)
                                    {
                                        CacheSet.CacheServers = VelocityServers(xn);
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
            if ((CacheSet.CacheServers == null) || (CacheSet.CacheServers.Count == 0))
            {
                throw new Exception("缓存配置文件错误!");
            }
            return CacheSet;

        }

        /// <summary>
        /// 读取缓存服务器地址
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private List<CacheServer> VelocityServers(XmlNode node)
        {
            List<CacheServer> list = new List<CacheServer>();

            CacheServer serv = null;
            foreach (XmlNode xn in node)
            {
                if (xn.Name == "CacheServer")
                {
                    serv = new CacheServer();
                    serv.HostName = XmlAttributes(xn, "HostName");

                    serv.CachePort = XmlAttributes(xn, "CachePort").To<int>(serv.CachePort);

                    list.Add(serv);
                }
            }

            return list;
        }

        private string XmlAttributes(XmlNode xn, string AttributesName)
        {
            XmlAttribute att = xn.Attributes[AttributesName];
            return att.Value;
        }

    }
}
